from flask import render_template, jsonify
from flask import request, session

from models import db
from models.index import News, User, Comment, Category
from . import index_blu



@index_blu.route("/")
def index():
    clicks_top_6_news = db.session.query(News).order_by(-News.clicks).limit(6)
    # return render_template("./index/index.html", clicks_top_6_news=clicks_top_6_news)

    user_id = session.get('user_id', 0)
    nick_name = session.get('nick_name', '')
    user = db.session.query(User).filter(User.id == user_id).first()
    categorys = db.session.query(Category).filter(Category.id != 1).all()
    return render_template("./index/index.html", clicks_top_6_news=clicks_top_6_news, nick_name=nick_name, user=user,
                           categorys=categorys)


@index_blu.route('/newslist')
def category_news():
    page = request.args.get('page', 1)  # 前端要的是哪一页的数据
    cid = int(request.args.get('cid', 0))  # 前端要的是哪个分类的数据，是股市、债市还是商品、外汇、公司
    per_page = request.args.get('per_page', 1)  # 前端要的是每一页中的新闻个数

    if cid == 1:
        paginate = db.session.query(News).filter(News.status == 0).order_by(-News.create_time).paginate(page=int(page),
                                                                                                        per_page=int(
                                                                                                            per_page),
                                                                                                        error_out=False)
    else:
        # cid += 1  # 由于测试数据分类中从0开始，而数据库中是从1开始的，所以用户点击的1实际上是2
        paginate = db.session.query(News).filter(News.category_id == cid, News.status == 0).order_by(
            -News.create_time).paginate(
            page=int(page), per_page=int(per_page), error_out=False)

    ret = {
        "totalPage": paginate.pages,  # 总页数
        "newsList": [news.to_dict() for news in paginate.items]
    }
    return jsonify(ret)


@index_blu.route("/detail/<int:news_id>")
def detail(news_id):
    # print('=========>',news_id)
    user_id = session.get('user_id', 0)
    nick_name = session.get('nick_name', '')

    news = db.session.query(News).filter(News.id == news_id).first()
    # 查看新闻作者
    news_author = news.user

    # print('=======>',news_author.nick_name)
    # 查看新闻总数
    news_author.news_num = news_author.news.count()
    # 查看粉丝数
    news_author.follwer_num = news_author.followers.count()

    # print('======>',nick_name)
    # 查询点击量最多的6个新闻信息
    click_detail_news_sex = db.session.query(News).order_by(-News.clicks).limit(6)
    user = db.session.query(User).filter(User.id == user_id).first()
    like_comment = user.like_comment
    comments = news.comments.order_by(-Comment.create_time)

    news_author_followers_id = [x.id for x in news_author.followers]
    if user_id in news_author_followers_id:
        news_author.can_follow = False  # 已经关注了作者，就不能在关注了
    else:
        news_author.can_follow = True  # 可以关注

    news_collected_user_id = [x.id for x in news.collected_user]
    if user_id in news_collected_user_id:
        news.can_collect = False  # 已经收藏了，就不要在收藏了
    else:
        news.can_collect = True  # 可以收藏

    return render_template("./index/detail.html", news=news, nick_name=nick_name, news_author=news_author,
                           click_detail_news_sex=click_detail_news_sex, like_comment=like_comment, comments=comments)
